<?php
require_once "config.php";
require_once "util.php";

connect_db();

if (isset($_POST['new-comment'])) {
    $comment = trim($_POST['new-comment']);
    $sql = sprintf('insert into bad_word(content) values("%s")',
                   $comment);
    mysql_query($sql);

} else if (isset($_POST['del-id'])) {
  $sql = sprintf('delete  from bad_word where id = %d',
                 $_POST['del-id']);
    mysql_query($sql);
    return;
}


header ('Content-type: text/html; charset=utf-8');



?>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.3.0/build/cssreset/reset-min.css">
    <title>Add random comment | Help Make Taras Robust</title>

    <script src="/lib/js/jquery.js"></script>
    <script src="/lib/js/sprintf.js"></script>

    <script type="text/javascript">
    function remove(anchor) {
        var span = $(anchor).parent();
        var id = $(span).find('span.comment-id').html();
        $.ajax({
            url: '#',
            type: 'POST',
            data: {'del-id': id, 'type': $(anchor).attr('type')},
            success: function(data) {
                window.location = 'manage_bad_word.php';
            },
              error: function(jqXHR, status) {
                alert('删除失败:' + jqXHR.responseText);
            }
        });
        //$(anchor).parent().remove();
    }
    </script>

    <style type="text/css">
      form {
        margin: 20px 20px;
      }

      div.comment-soup {
          margin: 5px 20px;
      }

      span.comment-id {
          display: none;
      }

      span.comment {
          display: inline-block;
          margin-right: 30px;
          margin-bottom: 5px;
          border: solid 1px #ababab;
          padding: 0px 5px;
      }
    </style>

  </head>
  <body>
    <form action="#" method="post">
      <input type="text" name="new-comment" />
      <input type="submit" value="添加" />
    </form>

    <div class="comment-soup">
      <?php
        $exists_comments = mysql_query('select * from bad_word');

     while ($comment = mysql_fetch_assoc($exists_comments)) {
       echo sprintf("<span class='comment'><span class='comment-id'>%d</span> 
<span class='comment-content'>%s</span><a href='#' type='comment' onclick='remove(this)'>X</a></span>", $comment['id'], $comment['content']);
     }
      ?>
    </div>

  </body>
</html>
